package com.clouddo.clouddologin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.clouddo.clouddologin.model.User;
import com.clouddo.clouddologin.service.UserService;


@RequestMapping(value = "/user")
@RestController
public class UserController {
	
	public UserController() {
		// TODO Auto-generated constructor stub
	}
	
	@Autowired
	UserService userService;
	
	@GetMapping("{username}")
	@CrossOrigin
	String get(@PathVariable("username") String username){
		return userService.get(username);
	}

	@RequestMapping("/list")
	@Produces("application/json;charset=UTF-8")
	@CrossOrigin
	public Map<String, Object> list() {
		List<User> userlist = userService.list();
		Map<String, Object> map = new HashMap<>();
		map.put("results", userlist);
		return map;
	}
	
	@RequestMapping(value = "/save")
	@Produces("application/json;charset=UTF-8")
	@CrossOrigin
    int save(@RequestBody User user) {
		return userService.save(user);
	}
	
	@RequestMapping(value="/update")
	@Produces("application/json;charset=UTF-8")
	@CrossOrigin
	int update(@RequestBody User user) {
		return userService.update(user);
	}
	
	@RequestMapping(value="/delete")
	@Produces("application/json;charset=UTF-8")
	@CrossOrigin
	int remove(@RequestBody String name) {
		return userService.remove(name);
	}

}
